$(document).ready(function()
    {

        $.clear_form = function(id)
        {
            $('#'+id).each(function(){
                this.reset();
            });
        }
        
        //    Set value all component in container with list_data
        //
        //    Sandi Rizky Kharisma (sandi.informatics08@gmail.com)
        //
        //    _container : panel to display result (div, form, etc)
        //    _list_data : data of array, NAME ELEMENT Must equal to name Variabel
    
        $.set_data = function(container, list_data)
        {
            $(container)[0].reset();
                    
            $(container).each(function(){
                for(var i = 0; i < this.elements.length; i++)
                {
                    var currentElement = this.elements[i];
                    if (typeof list_data[currentElement.name] != 'undefined') {
                        if(currentElement.type == "checkbox" || currentElement.type == "radio")
                        {
                            $(currentElement + "[value=" + list_data[currentElement.name]+ "]").attr('checked', list_data[currentElement.name]);
                        }else{
                            $(currentElement).val(list_data[currentElement.name]);
                        }
                    }
                }
            });
        }

        //    Minimalize Calling Ajax Function
        //
        //    Sandi Rizky Kharisma (sandi.informatics08@gmail.com)
        //
        //    _url : class/function (base_url is define in head html as [CI] base_url())
        //    _source_data : name form / data of array
        //    _container : panel to display result (div, form, etc)
        //    _callback : if ajax success will call function callback and also passing data result
        //
        //    example :
        //      1) params 2 using id Form & without callback function
        //     
        //      var data = {
        //               'WEB_NAME' : "sandi",
        //               'WEB_DESCRIPTION' : "sandi desc"
        //          };
        //      $.call_ajax('example/ajax/', data, "#test_form", $.callback_func);

        $.call_ajax = function(_url, _source_data, _container)
        {
            var _data = $(_source_data).serialize();
            if(!_data)
                _data = _source_data;

            $.ajax({
                url: base_url + _url,
                data : _data,
                type: 'post',
                success : function(data)
                {
                    if(_container != null)
                    {
                        if(data.data.length == 1)
                            $.set_data(_container, data.data[0]);
                    }
                        
                },
                error: function(res)
                {
                 //   alert(res.responseText);
                },
                dataType: 'json'
            });
        }

        $.data_ajax = function(_url, _source_data, _callback)
        {
            var _data = $(_source_data).serialize();
            if(!_data)
                _data = _source_data;
            
            $.ajax({
                url: base_url + _url,
                data : _data,
                type: 'post',
                success : function(data)
                {
                    _callback(data);
                },
                error: function(res)
                {
                    //alert(res.responseText);
                },
                dataType: 'json'
            });
        }


        //    Minimalize Calling DataTable Function
        //      + Not all data loaded, only on preview
        //      + good for many data
        //      - only simple query
        //
        //    Sandi Rizky Kharisma (sandi.informatics08@gmail.com)
        //
        //    _url : class/function (base_url is define in head html as [CI] base_url())
        //    id_table : id of table

        $.table_master = function(id_table, url)
        {
            return $(id_table).dataTable({
                "bJQueryUI": true,
                "sPaginationType": "full_numbers",
                "bProcessing": true,
                "bServerSide": true,
                "sAjaxSource": base_url + url,
                "bRetrieve" :true,
                "fnServerData": function ( sSource, aoData, fnCallback ) {
                    $.ajax( {
                        "dataType": 'json',
                        "type": "POST",
                        "url": sSource,
                        "data": aoData,
                        "success": fnCallback,
                        "error" : function(res)
                        {
                     //       alert(res.responseText);
                        },
                        "complete" : function()
                        {
                        
                        }
                    } );
                }
            });
        }


        //    Minimalize Calling DataTable Function
        //      + complex query
        //      - bad for big data
        //
        //    Sandi Rizky Kharisma (sandi.informatics08@gmail.com)
        //
        //    _url : class/function (base_url is define in head html as [CI] base_url())
        //    id_table : id of table
    
        $.table_data = function(_url, _source_data, id_table, column)
        {
            $.ajax({
                url: base_url + _url,
                data : _source_data,
                type: 'post',
                success : function(data)
                {
                    return $(id_table).dataTable({
                        "bJQueryUI": true,
                        "sPaginationType": "full_numbers",
                        "aaData": data.data,
                        "aoColumns":column,
                        "aoColumnDefs": [
                        //                        {
                        //                            "bVisible": false,
                        //                            "aTargets": [ 0 ]
                        //                        },
                        {
                            "sClass": "center",
                            "aTargets": [ 1 ]
                        }
                        ]
                    });
                },
                error: function(res)
                {
                   // alert(res.responseText);
                },
                dataType: 'json'
            });
        }
    });